package vmcs.controller;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto, Pang Pingli
//

import java.util.Observable;
import java.util.Observer;
import vmcs.MoneyFormatter;
import vmcs.common.Constant;
import vmcs.common.VMCSException;
import vmcs.maintenance.AccessManager;
import vmcs.store.CashStoreItem;
import vmcs.store.DrinksBrand;
import vmcs.store.DrinksStoreItem;
import vmcs.store.Store;
import vmcs.store.StoreController;
import vmcs.ui.interfacecontrol.*;

public class MaintenanceController implements Observer{

    private MainController       mCtrl;
    private IMaintenancePanel     mpanel;
    private AccessManager        am;
    private static int			coinIdx=-1;
    private static int			drinkIdx=-1;
    public MaintenanceController (MainController  mctrl) {
      mCtrl = mctrl;
      am = new AccessManager(this);
    }

    public MainController  getMainController(){
      return mCtrl;
    }

    public IMaintenancePanel    getMaintenancePanel(){
      return mpanel;
    }

    /**
     * setup the maintenance panel and display it.
     */
    public void displayMaintenancePanel(){
      ISimulatorPanel  scp = mCtrl.getSimulatorControlPanel();
      if(mpanel == null)
        mpanel = UIController.getMaintenancePanel(scp, this);
      mpanel.display();
      mpanel.setMaintenancePanelActive(Constant.DIALOG, true);  // setActive of password, invalid and valid display.
    }

    public AccessManager getAccessManager(){
      return am;
    }

    public void loginMaintainer(boolean st){
      mpanel.displayPasswordState(st);
      mpanel.clearPassword();
      if(st == true){
        // login successful
        mpanel.setMaintenancePanelActive(Constant.WORKING, true);
        mpanel.setMaintenancePanelActive(Constant.PSWD, false);

        mCtrl.getMachineryController().setDoorState(false);
        ISimulatorPanel      scp = mCtrl.getSimulatorControlPanel();
        scp.setPanelActive(Constant.ACT_CUSTOMER, false);
      }
    }

    // invoked in CoinDisplayListener
    public void displayCoin(int idx) {
      StoreController    sctrl = mCtrl.getStoreController();
      CashStoreItem      item;
      coinIdx=idx;
      System.out.println("coinIdx= " +idx);
      try {
        item = (CashStoreItem) sctrl.getStoreItem(Store.CASH, idx);
        mpanel.setCoinDisplayQuantity(idx, item.getQuantity());
      } catch (VMCSException e){
        System.out.println("MaintenanceController.displayCoin:" + e);
      }

  }

    // invoked in DrinkDisplayListener;
      public void displayDrinks(int idx) {
      StoreController    sctrl = mCtrl.getStoreController();
      DrinksStoreItem      item;
      drinkIdx = idx;
      System.out.println("drinkIdx= " +idx);
      try {
        item = (DrinksStoreItem) sctrl.getStoreItem(Store.DRINK, idx);
       // mapnel.getDrinkDisplay().displayQuantity(idx, item.getQuantity());
        DrinksBrand  db = (DrinksBrand) item.getContent();
        mpanel.setDrinkDisplayQuantity(idx, item.getQuantity());
        mpanel.displayPrice(db.getPrice());
      } catch (VMCSException e){
        System.out.println("MaintenanceController.displayDrink:" + e);
      }

  }

  // invoked by PriceDisplayListener
  public void setPrice(int pr){
    StoreController    sctrl = mCtrl.getStoreController();
  //  TransactionController  tctrl = mCtrl.getTransactionController();

    int   curIdx;

    curIdx = mpanel.getCurIdx();
    try {
      sctrl.setPrice(curIdx, pr);
      mpanel.setPriceDisplayValue(MoneyFormatter.toMoney(pr));
     // tctrl.setPrice(curIdx, pr);
    } catch (VMCSException e){
      System.out.println("MaintenanceController.setPrice:" + e);
    }
  }

  // TotalCashButtonListener
  public void getTotalCash(){
    StoreController    sctrl = mCtrl.getStoreController();

    int  tc;

    try {
      tc = sctrl.getTotalCash();
      mpanel.displayTotalCash(tc);

    } catch (VMCSException e){
      System.out.println("MaintenanceController.getTotalCash:" + e);
    }
  }

  // TransferCashButtonListener
  // get all the cash from store and set store cash 0;
  public void transferAll(){
     StoreController    sctrl = mCtrl.getStoreController();

     int  cc;   // coin quantity;

   try {

      cc = sctrl.transferAll();
      mpanel.displayCoins(cc);
      mCtrl.getMachineryController().displayCoinStock();
      //sctrl.updateStoreDisplay(Store.CASH);
      // the cash qty current is displayed in the Maintenance panel needs to be update to be 0;
      // not required.
      mpanel.updateCurrentQtyDisplay(Store.CASH, 0);
    } catch (VMCSException e){
      System.out.println("MaintenanceController.transferAll:" + e);
    }
  }

  // StoreViewerListener, not required;
  public void changeStoreQty(char type, int idx, int qty){

     try {
      mpanel.updateQtyDisplay(type, idx, qty);
      mpanel.initCollectCash();
      mpanel.initTotalCash();
     } catch (VMCSException e){
      System.out.println("MaintenanceController.changeStoreQty:" + e);
    }
  }

  // exit button listener;
  public void logoutMaintainer(){

    StoreController    sctrl = mCtrl.getStoreController();

     boolean  ds = sctrl.isDoorClosed();

     if(ds == false) {
    	 mpanel.showMessage(Constant.LOCK_DOOR);
          return;
    }

    mpanel.setMaintenancePanelActive(Constant.DIALOG, true);

  }

  public boolean closeDown(){
    StoreController    sctrl = mCtrl.getStoreController();
    if(mpanel == null)
      return true;
    if(sctrl.isDoorClosed() == false){
    	  mpanel.showMessage(Constant.LOCK_DOOR);
          return false;
    }
    mpanel.closeDown();
    ISimulatorPanel     smctrl = mCtrl.getSimulatorControlPanel();
    smctrl.setPanelActive(Constant.ACT_MAINTAINER, true);
    smctrl.setPanelActive(Constant.ACT_CUSTOMER, true);
    return true;
  }

@Override
public void update(Observable o, Object arg) {
	System.out.println("Update detected: " + this.toString());
	System.out.println("Object: "+arg.toString());
	if(coinIdx!=-1){
		displayCoin(coinIdx);
	}
	if(drinkIdx!=-1){
		displayDrinks(drinkIdx);
	}
}


}